<?php
/**
 * @author Laurent SITTLER <lolo.sittler@gmail.com>
 * @author Alexandre GOMEZ <gomez.alexandre02@gmail.com>
 * @version 0.1
 */
/**
 *  View manuel request
 */
?>
<h3>Requ&ecirc;te SQL</h3>
<?php
// $userReq = new User();
$checkUser = new User_Model();
$userReq = $checkUser->GetUserById(Session::Get(Constants::$SESSION_cuser));
if (Session::Get(Constants::$SESSION_cdb) == NULL && $userReq->GetAdmin() == 0) {
    echo '<div class = "ui-widget">';
    echo '<div class = "ui-state-error ui-corner-all" style = "margin-top: 20px; padding: 0 .7em;">';
    echo '<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>';
    echo 'Veuillez séléctionner une base de données !</p>';
    echo '</div>';
    echo '</div>';
} else {
    ?>
    <form action="<?php echo $GLOBALS['conf']['interface']['url']; ?>index/nrequete" method="post">
        <fieldset>
            <div class="legend"><span class="sql">&nbsp;</span>Exécuter une requête SQL :</div>
            <?php
            if (Session::Get(Constants::$SESSION_sql) != NULL)
                $request = Session::Get(Constants::$SESSION_sql);
            else
                $request = NULL;
            echo '<textarea class="lined" rows="9" cols="60" name="tRequest">' . $request . '</textarea>';
            ?>
            <input type="hidden" name="current-tab" value="<?php echo $j; ?>" />
            <input type="submit" name="exec" value="Executer" />
        </fieldset>
    </form>
    <br />
    <?php
    if (Session::Get(Constants::$SESSION_resultRequest) != NULL) {
        $table = Session::Get(Constants::$SESSION_resultRequest);
        echo '<div style="max-width: 100%;height: auto;overflow-x: auto;">';
        echo '<table class="tableContent">';
        echo '<tr>';
// Header table
        foreach (array_keys($table[0]) as $key) {
            echo '<th>' . $key . '</th>';
            $i++;
        }
        echo '</tr>';
// Content tabel
        $linReq = 0;
        foreach ($table as $tb) {
            if ($linReq % 2)
                echo '<tr class="style">';
            else
                echo '<tr>';
            foreach ($tb as $value) {
                echo '<td>' . $value . '</td>';
            }
            echo '</tr>';
            $linReq++;
        }
        echo '</table>';
        echo '</div>';
    }
}
?>
<script type="text/javascript">
    $(function() {
        $(".lined").linedtextarea({
            selectedLine: 1
        });
    });
</script>